package com.wwwarehouse.common.tools.rsa;

import android.content.Context;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * String Utils
 * Created by ShineF on 2017/8/17 0017.
 */

public class StringUtils {

    /**
     * 获得Html格式的Spanned对象
     *
     * @param context
     * @param id
     * @param objects
     * @return
     */
    public static Spanned getSpannedHtml(Context context, int id, Object... objects) {
        return Html.fromHtml(String.format(context.getResources().getString(id), objects));
    }

    /**
     * 获得string文件中的字符串
     *
     * @param context
     * @param resId
     * @param args
     * @return
     */
    public static String getResourceStr(Context context, int resId, Object... args) {
        return String.format(context.getResources().getString(resId), args);
    }

    /**
     * textview中部分字体的颜色,没有超链接的
     *
     * @param text      字符串
     * @param colorlist 颜色集合
     * @param mapindex  开始结束的下标map
     * @return
     */
    public static SpannableStringBuilder setTextColor(String text, List<Integer> colorlist, Map<Integer, Integer> mapindex) {
        SpannableStringBuilder builder = new SpannableStringBuilder(text);
        List<Integer> startlist = new ArrayList<Integer>();
        List<Integer> endlist = new ArrayList<Integer>();
        for (int key : mapindex.keySet()) {
            startlist.add(key);
            endlist.add(mapindex.get(key));
        }
        ForegroundColorSpan redSpan = null;
        for (int i = 0; i < mapindex.size(); i++) {
            if (colorlist.size() == 1) {
                redSpan = new ForegroundColorSpan(colorlist.get(0));
            } else {
                redSpan = new ForegroundColorSpan(colorlist.get(i));
            }
            builder.setSpan(redSpan, startlist.get(i), endlist.get(i), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return builder;
    }

    /**
     * 判断字符串是否为空或null
     *
     * @param str
     * @return
     */
    public static boolean isNullString(String str) {
        return TextUtils.isEmpty(str)  || str.equals("null");
    }

    public static boolean isNoneNullString(String str) {
        return !isNullString(str);
    }

    /**
     * 阿拉伯数字转换成中文
     * @param string
     * @return
     */
    public static String numToChinese(String string) {
        String[] s1 = { "零", "一", "二", "三", "四", "五", "六", "日", "八", "九" };
        String[] s2 = { "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" };

        String result = "";

        int n = string.length();
        for (int i = 0; i < n; i++) {

            int num = string.charAt(i) - '0';

            if (i != n - 1 && num != 0) {
                result += s1[num] + s2[n - 2 - i];
            } else {
                result += s1[num];
            }
            System.out.println("  "+result);
        }

        System.out.println("----------------");
        System.out.println(result);
        return result;

    }

}
